/**
 * 
 */
package com.acc.softmgr.common.genertor.gen;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.velocity.VelocityContext;

import com.acc.softmgr.common.genertor.bean.ColumnData;
import com.acc.softmgr.common.genertor.utils.CodeResourceUtil;
import com.acc.softmgr.common.genertor.utils.CommUtil;
import com.acc.softmgr.common.genertor.utils.PageParserUtil;

/**
 * 
 * @content 生成实体example
 * 
 * @date 2017年5月20日
 * 
 * @author OprCalf
 * 
 */
public class GenerateEntityExample extends BaseGenerate {

	public static void genEntityExampl(String tableName, String pckPath, String beanPath) {
		String className = CommUtil.formatTableNameToClassName(tableName);
		VelocityContext context = new VelocityContext();
		context.put("basePackage", CodeResourceUtil.basePackage);
		context.put("createDate", new Date());
		context.put("className", className);
		try {
			context.put("listCondition", getListCondition(tableName));
		} catch (Exception e) {
			e.printStackTrace();
		}
		PageParserUtil.WriterPage(context, "EntityExampleTemplate.ftl", pckPath, beanPath);
	}

	private static String getListCondition(String tableName) throws SQLException {

		List<ColumnData> dataList = createBean.getColumnDatas(tableName);
		StringBuffer str = new StringBuffer();

		for (ColumnData d : dataList) {
			String lowName = CommUtil.formatName(d.getColumnName());
			String maxName = lowName.substring(0, 1).toUpperCase() + lowName.substring(1, lowName.length());
			String type = d.getDataType();

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "IsNull() {");
			str.append("\r\t");
			str.append("	    addCriterion(\"" + lowName + " is null\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "IsNotNull() {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " is not null\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "EqualTo(" + type + " value) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " =\", value, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "NotEqualTo(" + type + " value) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " <>\", value, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "GreaterThan(" + type + " value) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " >\", value, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "GreaterThanOrEqualTo(" + type + " value) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " >=\", value, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "LessThan(" + type + " value) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " <\", value, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "LessThanOrEqualTo(" + type + " value) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " <=\", value, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "In(List<" + type + "> values) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " in\", values, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "NotIn(List<" + type + "> values) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " not in\", values, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "Between(" + type + " value1, " + type + " value2) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " between\", value1, value2, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

			str.append("\r\t");
			str.append("    public Criteria and" + maxName + "NotBetween(" + type + " value1, " + type + " value2) {");
			str.append("\r\t");
			str.append("        addCriterion(\"" + lowName + " not between\", value1, value2, \"" + lowName + "\");");
			str.append("\r\t");
			str.append("        return (Criteria) this;");
			str.append("\r\t");
			str.append("    }");
			str.append("\r\t");

		}
		return str.toString();
	}

}
